<template>
  <el-card style="height: 86vh">
    <el-row>
      <el-col>
        <el-date-picker
          v-model="gkDate"
          type="date"
          @change="handleTimePickChange"
          value-format="YYYY-MM-DD"
          format="YYYY年MM月DD日"
          placeholder="选择日期"
        >
        </el-date-picker>
      </el-col>
    </el-row>

    <el-input
      type="textarea"
      style="margin-top: 20px"
      :rows="16"
      v-model="gkDesc"
      v-loading="dataLoading"
    ></el-input>
    <el-button type="primary" style="margin-top: 20px" @click="copy"
      >复制</el-button
    >
  </el-card>
</template>
<script setup>
import { onMounted, ref } from "vue";
import utils from "@/utils/utils";
import store from "@/store";
import {
  createDailyData,
  getAll,
  getComplaintData,
  getComp,
} from "@/api/work-order";
import { isSameDate } from "@/utils/common";

let headers = {};
const upLoadLoading = ref(false);
const UploadExcelDialogShow = ref(false);
const total = ref(0);
const page = ref(1);
const size = ref(10);
const tableData = ref([]);
const dataLoading = ref(false);
const selectTimeDialogShow = ref(false);
const timeModelValue = ref("");
const gkDate = ref("");
const gkDesc = ref("");

const truncateString = (str, maxLength = 10) => {
  if (str.length > maxLength) {
    return str.substring(0, maxLength) + "...";
  }
  return str;
};

const getData = async () => {
  dataLoading.value = true;
  const result = await getComp({
    time: gkDate.value,
  });

  gkDesc.value = generateDailyReport(result,gkDate.value);
  dataLoading.value = false;
};

// const getTime = async () => {
//   gkDate.value = (await getComp()).substring(0, 10);
// };
// getTime().then(() => {
//   getData();
// });

const handleProgress = (file) => {
  upLoadLoading.value = true;
};

const beforeUploadHandle = (file) => {
  utils.showLoadding("正在上传...");
  return true;
};

const handleSuccess = async (res) => {
  if (res.content === 0) {
    utils.showSuccess("上传数据成功");
  }

  UploadExcelDialogShow.value = false;
  utils.hideLoadding();
};
const handleTimePickChange = (value) => {
  gkDate.value = value;
  getData();
};
const copy = () => {
  navigator.clipboard.writeText(gkDesc.value);
  utils.showSuccess("复制成功");
};

const generateDailyReport = (data,dateStr) => {
  
  const date = new Date(dateStr);
  const dateText = `${date.getMonth() + 1}月${date.getDate()}日投诉明细`;

  // 工具函数
  const groupBy = (arr, key) =>
    arr.reduce((acc, item) => {
      (acc[item[key]] = acc[item[key]] || []).push(item);
      return acc;
    }, {});

  const summarize = (arr) =>
    arr
      .map(
        (item) => `${item.cityBureau}${item.num > 1 ? item.num + "宗" : "1宗"}`
      )
      .join("、");
  const summarizeBusiness1 = (arr) =>
    arr.map((item, index) => `${index + 1}. ${item}`);

  // const summarizeBusiness = (arr) =>
  //   arr.map((item) => `${item.businessName}${item.sum}宗`).join("，");
  // 一次投诉统计
  const detail95598Total = data.detail95598;
  const detail95598RepeatTotal = data.detail95598Repeat;
  const detail12398Total = data.detail12398;
  const detail12398AppealTotal = data.detail12398Appeal;//申诉不需要总数
  const detail95598Text =
  data.detail95598Count > 0
      ? `95598一次投诉工单${data.detail95598Count}宗`
      : `95598一次投诉0宗`;
  const detail95598RepeatText =
    data.detail95598RepeatCount > 0
      ? `95598重复投诉${data.detail95598RepeatCount}宗`
      : `95598重复投诉0宗`;
  const detail12398Text =
  data.detail12398Count> 0
      ? `12398投诉工单 ${data.detail12398Count}宗`
      : `12398投诉工单0宗`;

  return `${dateText}\n\n一、${detail95598Text}\n${summarizeBusiness1(detail95598Total).join(
    "\n"
  )}\n\n二、${detail95598RepeatText}\n${summarizeBusiness1(
    detail95598RepeatTotal
  ).join("\n")}\n\n三、${detail12398Text}\n${summarizeBusiness1(
    detail12398Total
  ).join("\n")}\n\n四、12398投诉申诉工单${data.detail12398AppealCount}宗\n${summarizeBusiness1(detail12398AppealTotal).join(
    "\n"
  )}`;
};
onMounted(() => {
  headers = { "X-Token": store.getters.token };
});
</script>
